﻿using CommunityToolkit.Mvvm.Input;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using UpperComputer.WPF.Entry.Models;
using UpperComputer.WPF.Entry.Views;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class MenuButtonOperationViewModel : OperationViewModel
    {
        private readonly IBaseMenuButtonService _menuButtonService;

        public IAsyncRelayCommand MenuSelectCommand { get; }

        public NameValueModel<bool>[] IsActives { get; set; }

        private string _menuNmae;
        [Required(ErrorMessage = "请选择菜单")]
        public string MenuName
        {
            get => _menuNmae;
            set => SetProperty(ref _menuNmae, value);
        }

        private BaseMenuButtonDto _menuButton = new BaseMenuButtonDto();
        public BaseMenuButtonDto MenuButton
        {
            get => _menuButton;
            set => SetProperty(ref _menuButton, value);
        }

        public MenuButtonOperationViewModel(IBaseMenuButtonService menuButtonService, long id)
        {
            _menuButtonService = menuButtonService;
            Id = id;
            MenuSelectCommand = new AsyncRelayCommand(MenuSelectCommand_Execute);
            IsActives = new NameValueModel<bool>[] {
                new NameValueModel<bool>(Lang["是"], true),
                new NameValueModel<bool>(Lang["否"], false)
            };
        }

        private async Task MenuSelectCommand_Execute(CancellationToken cancellationToken)
        {
            MenuSelectControl menuSelectControl = Furion.App.GetService<MenuSelectControl>();
            object result = await menuSelectControl.ShowCustomDialog(Constant.DIALOG_IDENTIFIER_MENU);
            if (result != null && result is BaseMenuResultDto)
            {
                BaseMenuResultDto menu = (BaseMenuResultDto)result;
                MenuName = menu.DisplayName;
                MenuButton.MenuCode = menu.Code;
            }
        }

        protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
        {
            if (!IsAdd)
            {
                BaseMenuButtonResultDto menuButton = await _menuButtonService.Get(Id, cancellationToken);
                MenuName = menuButton.MenuName;
                MenuButton = menuButton;
            }
        }

        protected override async Task OkAsync(object parameter, CancellationToken cancellationToken)
        {
            if (!IsValidated || !MenuButton.IsValidated)
            {
                return;
            }
            await TryExecuteCommandAysnc(async cancellationToken =>
            {
                _ = IsAdd ? await _menuButtonService.Post(MenuButton, cancellationToken) : await _menuButtonService.Put(MenuButton, cancellationToken);
                CloseRootDialog(true);
            }, cancellationToken);
        }
    }
}
